package com.jjh.exception;

import com.jjh.consts.ResEnum;
import com.jjh.consts.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 全局异常处理器
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 空指针异常处理
     * @param e 异常
     * @return   异常信息
     */
    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public Result error(NullPointerException e){
        return new Result(ResEnum.ERROR,e.getMessage());
    }

    /**
     * token异常
     * @param e
     * @return
     */
    @ExceptionHandler(TokenException.class)
    @ResponseBody
    public Result TokenError(TokenException e){
        System.out.println("捕获到token异常...");
        return new Result(e);
    }

    /**
     * 用户未认证错误
     * @param e
     * @return
     */
    @ExceptionHandler(UnAuthenticationException.class)
    @ResponseBody
    public Result UnAuthenticationError(UnAuthenticationException e){
        return new Result(ResEnum.UN_AUTHENTICATION_ERROR,e.getMessage());
    }

    /**
     * 文件上传错误
     * @param e
     * @return
     */
    @ExceptionHandler(FileException.class)
    @ResponseBody
    public Result FileError(FileException e){
        return new Result(e);
    }

    /**
     * 删除关联外键错误
     * @param e
     * @return
     */
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public Result ForkeyError(SQLIntegrityConstraintViolationException e){
        return Result.error(e.getMessage());
    }





}
